package com.example.myapplication.antivity

import android.content.Context
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.example.mvp.view.BaseActivity
import com.example.myapplication.R
import com.example.myapplication.entity.Login_Entity
import com.example.youhuo.a_fragment.Fragment_Conteact
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.model.LjlModel
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.HttpType

class Login2Activity : BaseActivity<LjlPresenter>(), LjlContract.LjlView,Fragment_Conteact {


    private val logName: EditText by lazy { findViewById<EditText>(R.id.log_name) }
    private val logPass: EditText by lazy { findViewById<EditText>(R.id.log_pass) }
    private val logButt: Button by lazy { findViewById<Button>(R.id.log_butt) }
    var parmas = ArrayList<Map<String, Any>>()
    override fun findlayout(): Int {
        return R.layout.activity_login2

    }


    override fun initView() {

    }

    override fun initData() {
        loginbutton()

    }

    fun loginbutton() {
        logButt.setOnClickListener(View.OnClickListener {
            var name = logName.text.toString().trim()
            var pass = logPass.text.toString().trim()

            setlogrequest(name, pass)


        })
    }

    fun setlogrequest(name: String, pass: String) {
        mypre = LjlPresenter(LjlModel(), this)
        var map: HashMap<String, Any> = HashMap()
        map.put("code", HttpType.LOGIN)
        map.put("username", name)
        map.put("password", pass)
        parmas.add(map)

        mypre.TabData(parmas)
    }

    override fun showmsg(entity: BaseEntity) {
        if (entity.msg.equals("请求成功")) {
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show()
            var loginEntity = entity as Login_Entity
            var values = loginEntity.values
            var userName = values?.get(0)?.user_name


//            var intent = Intent(this,HomeActivity::class.java)
//            intent.putExtra("name",userName)
//            startActivity(intent)

            getSharedPreferences("islog", Context.MODE_PRIVATE).edit().putBoolean("log",true).commit()


            finish()

        }else{
            Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show()
        }
    }
    override fun showFragment(type: Fragment_Conteact.FragmentType) {

    }


}
